<template>
	<view class="news">
		<newsItem :list="newsList" @itemClick="goDetail"></newsItem>
	</view>
</template>

<script>
import newsItem from '../../components/news-item/news-item.vue'
export default {
	data() {
		return {
			newsList: []
		}
	},
	components: {
		newsItem
	},
	onLoad () {
		this.getNews()
	},
	// 下拉刷新，重置数据,在获取数据列表后拿到回调函数停止刷新
	onPullDownRefresh() {
		this.newsList = []
		setTimeout(()=>{
			this.getNews()
			uni.stopPullDownRefresh()
		},1000)
	},
	methods: {
		// 发送请求获取数据
		async getNews () {
			const res = await this.$myRequest({
				url: '/api/getnewslist'
			})
			this.newsList = res.data.message
		},
		// 通过子组件传的id跳转详情页面，url中问号可以起到连接作用
		goDetail (id) {
			uni.navigateTo ({
				url: '/pages/news-detail/news-detail?id=' + id
			})
		}
	}
}
</script>

<style lang="scss">
</style>
